import scrapy
from myscrapy.ItcastItem import ItcastItem

class ItcastSpider(scrapy.spiders.Spider):
    name = "itcast"
    allowed_domains = ["itcast.cn"]
    start_urls = (
        'http://www.itcast.cn/channel/teacher.shtml#ajavaee',
    )

    def parse(self, response):
        # open("teacher.html","w",encoding="utf-8").write(bytes.decode(response.body))
        # 存放老师信息的集合
        items = []
        for each in response.xpath("/html/body/div[1]/div[5]/div[2]/div[1]/ul/li"):
            # 将我们得到的数据封装到一个 `ItcastItem` 对象
            item = ItcastItem()
            # extract()方法返回的都是unicode字符串
            item['name'] = each.xpath("./div[2]/h3/text()").extract()[0]
            item['title'] = each.xpath("./div[2]/h4/text()").extract()[0]
            item['info'] = each.xpath("./div[2]/p/text()").extract()[0]
            items.append(item)
        # 直接返回最后数据
        # str = ("\n".join('%s' %item for item in items))
        # filename = "teacher.txt"
        # open(filename, 'w', encoding="utf-8").write(str)
        return items